package net.zdsoft.wpchat.support.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import net.zdsoft.keel.jdbc.JdbcBasicDao;
import net.zdsoft.keel.jdbc.MultiRowMapper;
import net.zdsoft.keel.util.Validators;
import net.zdsoft.wpchat.support.dao.OperatorGroupDao;
import net.zdsoft.wpchat.support.entity.OperatorGroup;

import org.springframework.stereotype.Repository;

/**
 * 用户对应群组关系dao实现
 *
 * @author xuan
 */
@Repository
public class OperatorGroupDaoImpl extends JdbcBasicDao implements OperatorGroupDao {
	private static final String SQL_FIND_BY_OPERATOR_ID = "SELECT "
			+ "operator_id, group_id, creation_time FROM su_group_operator WHERE operator_id=?";
	private static final String SQL_FIND_BY_GOURP_ID_IN = "SELECT "
			+ "operator_id, group_id, creation_time FROM su_group_operator WHERE group_id IN";
	private static final String SQL_INSERT_GROUP = "INSERT INTO su_group_operator"
			+ "(operator_id, group_id, creation_time) VALUES(?, ?, ?)";
	private static final String SQL_DELETE_BY_GROUP_ID_IN = "DELETE FROM "
			+ "su_group_operator WHERE group_id IN";
	private static final String SQL_DELETE_BY_OPERATOR_ID_IN = "DELETE FROM "
			+ "su_group_operator WHERE operator_id IN";

	@Override
	public List<OperatorGroup> findByOperatorId(String operId) {
		if (Validators.isEmpty(operId)) {
			return Collections.emptyList();
		}

		return query(SQL_FIND_BY_OPERATOR_ID, operId, new OperGroupMultiRowMapper());
	}

	@Override
	public List<OperatorGroup> findByGroupIds(String... groupIds) {
		if (Validators.isEmpty(groupIds)) {
			return Collections.emptyList();
		}
		return queryForInSQL(SQL_FIND_BY_GOURP_ID_IN, null, groupIds,
				new OperGroupMultiRowMapper());
	}

	@Override
	public void insertGroups(OperatorGroup... groups) {
		if (Validators.isEmpty(groups)) {
			return;
		}
		List<Object[]> listOfArgs = new ArrayList<Object[]>();
		for (OperatorGroup group : groups) {
			Object[] params = new Object[] { group.getOperatorId(),
					group.getGroupId(), new Date() };
			listOfArgs.add(params);
		}
		int[] types = new int[] { Types.CHAR, Types.CHAR, Types.TIMESTAMP };
		batchUpdate(SQL_INSERT_GROUP, listOfArgs, types);
	}

	@Override
	public void deleteByGroupIds(String... groupIds) {
		if (Validators.isEmpty(groupIds)) {
			return;
		}
		updateForInSQL(SQL_DELETE_BY_GROUP_ID_IN, null, groupIds);
	}

	@Override
	public void deleteByOperatorIds(String... operatorIds) {
		if (Validators.isEmpty(operatorIds)) {
			return;
		}
		updateForInSQL(SQL_DELETE_BY_OPERATOR_ID_IN, null, operatorIds);
	}

	/**
	 * 多条结果集
	 *
	 * @author xuan
	 */
	private static class OperGroupMultiRowMapper implements MultiRowMapper<OperatorGroup> {
		@Override
		public OperatorGroup mapRow(ResultSet rs, int rowNum) throws SQLException {
			OperatorGroup og = new OperatorGroup();
			og.setOperatorId(rs.getString("operator_id"));
			og.setGroupId(rs.getString("group_id"));
			og.setCreationTime(rs.getTimestamp("creation_time"));
			return og;
		}
	}

}
